<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{padding: 0;margin: 0;}
        .phone{
            width: 320px;
            height: 500px;
            margin: 50px auto;
            border: 1px solid #333;
            position: relative;
            text-align: center;
            line-height: 500px;
            user-select: none;
        }
        .slideBar{
            width: 100%;
            height: 50px;
            position: absolute;
            top: 60%;
            background-color: grey;
        }
        .ball{
            width: 50px;
            height: 50px;
            background-color:aquamarine;
            border-radius: 50%;
            position: absolute;
            left: 0px;
            top: 0;
        }
    </style>
</head>
<body>
    <div class="phone">
        <div class="slideBar">
            <div class="ball"></div>
        </div>
    </div>
</body>
<script>

    var ball = document.querySelector(".ball");
    var slideBar = document.querySelector(".slideBar");
    var phone = document.querySelector(".phone");

    var maxLeft = slideBar.clientWidth - ball.clientWidth;

    ball.onmousedown = function(e){
        e = e || window.event;

        var posX = e.offsetX;
        var posY = e.offsetY;
        console.log(posX,posY);
        
        document.onmousemove = function(e){
            var x = e.pageX - phone.offsetLeft - posX;

            if(x < 0) x = 0;
            if(x > maxLeft) x = maxLeft;

            ball.style.left = x + "px";

            // 移动完毕
            if(x == maxLeft){
                phone.style.backgroundColor = "#6ff";
                phone.innerHTML = "解锁成功";
            }

        }

        document.onmouseup = function(){
            document.onmousemove = null;
            // 松开时
            if(ball.offsetLeft != maxLeft){
                ball.style.left = 0 + "px";
            }

        }
    }


</script>
</html>